package com.template.util;

import java.util.HashMap;

/**
 *  用于 RESTFUL 返回
 */
public class RMap extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public enum RCode {
        OK(200), // 成功
        NOFOUND(404), //没有
        UNKNOWN(500);//未知异常
        private int code;

        private RCode(int code) {
            this.code = code;
        }

        public int value() {
            return this.code;
        }
    }

    public RMap() {
        put("code", RCode.OK.value());
    }

    public static RMap error() {
        return error(RCode.UNKNOWN, "未知异常，请联系管理员");
    }

    public static RMap error(Object msg) {
        return error(RCode.UNKNOWN, msg);
    }

    public static RMap error(RCode code, Object msg) {
        RMap r = new RMap();
        r.put("code", code.value());
        r.put("msg", msg);
        return r;
    }

    public static RMap ok(Object msg) {
        RMap r = new RMap();
        r.put("msg", msg);
        return r;
    }

    public static RMap ok() {
        return new RMap();
    }

    @Override
    public RMap put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
